package com.gserver.netty;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.execution.ExecutionHandler;
import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
import org.jboss.netty.handler.timeout.IdleStateHandler;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timer;

import com.gserver.netty.coder.ObjectDecoder;
import com.gserver.netty.coder.ObjectEncoder;

/**
 * 
 * channelPipeline是一系列channelHandler的集合，他参照J2ee中的Intercepting Filter模式来实现的，
 * 让用户完全掌握如果在一个handler中处理事件，同时让pipeline里面的多个handler可以相互交互
 */
public class ServerPipelineFactory implements ChannelPipelineFactory {
	public ServerHandler serverHandler;

	// public ChannelPipeline getPipeline() throws Exception {
	// ChannelPipeline pipeLine = Channels.pipeline();
	// pipeLine.addLast("decoder", new ObjectDecoder());
	// pipeLine.addLast("encoder", new ObjectEncoder());
	// pipeLine.addLast("handler", serverHandler);
	// return pipeLine;
	// }
	Timer hashedWheelTimer = new HashedWheelTimer();
	ExecutionHandler executionHandler = new ExecutionHandler(
			new OrderedMemoryAwareThreadPoolExecutor(16, 1048576, 1048576));

	public ChannelPipeline getPipeline() {
		return Channels.pipeline(
				new ObjectDecoder(), new ObjectEncoder(), executionHandler, // Must be shared
//				new ReadTimeoutHandler(hashedWheelTimer, 10),
//				new IdleStateHandler(hashedWheelTimer, 5, 5, 10),
				serverHandler);
	}

	public ServerHandler getServerHandler() {
		return serverHandler;
	}

	public void setServerHandler(ServerHandler serverHandler) {
		this.serverHandler = serverHandler;
	}

}
